﻿using SmartSpace.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Web;
using SmartSapce.ServiceLocation;

namespace SmartSpace.WebApp._console.page
{
    public partial class page_navigation_add : System.Web.UI.Page
    {
        public Guid _pageId;
        public Guid _parentId;

        protected void Page_Load(object sender, EventArgs e)
        {
            _pageId = Request.GetGuid("id");
            _parentId = Request.GetGuid("pid");

            _ctlLinkBack.NavigateUrl = string.Format("./page-navigation-list.aspx?id={0}", _pageId);
        }

        protected void _ctlBtnSave_Click(object sender, EventArgs e)
        {
            Nav navigation = new Nav();
            navigation.Id = Guid.NewGuid();
            navigation.SiteId = SsSite.Current.Id;
            navigation.PageId = _pageId;
            navigation.Code = _ctlEditName.Text;

            if (!string.IsNullOrEmpty(_ctlEditImage.Text))
            {
                navigation.ImageId = new Guid(_ctlEditImage.Text);
            }

            navigation.Caption = _ctlEditCaption.Text;
            navigation.Description = _ctlEditDescription.Text;
            navigation.ContainerCode = _ctlEditGroupName.Text;
            navigation.Arguments = _ctlEditArguments.Text;

            if (_parentId != default(Guid))
            {
                navigation.ParentId = _parentId;
            }

            if (radio_other.Checked && !string.IsNullOrEmpty(_ctlSelPageCode.Value))
            {
                navigation.TargetType = NavigationTargetType.Page;
                navigation.Url = _ctlSelPageCode.Value;
            }
            else
            {
                return;
                //if (_ctlTreeView.CheckedNodes.Count > 0)
                //{
                //    TreeNode selectedViewNode = _ctlTreeView.CheckedNodes[0];
                //    TreeNode selectedModuleNode = selectedViewNode.Parent;
                //    //menu.ModuleId = new Guid(selectedModuleNode.Value);
                //    menu.Url = string.Format("v:>>{0}", selectedViewNode.Value);
                //}
            }

            ICoreService service = ServiceLocator.Get<ICoreService>();
            service.SaveNavigation(navigation);
            Response.Redirect(_ctlLinkBack.NavigateUrl);
        }

        protected void _ctlReset_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("./page-navigation-add.aspx?id={0}", _pageId));
        }
    }
}